<template>
    <el-dialog class="create-mvc-dialog" title="生成MVC" width="85%"
               :visible.sync="visible" :before-close="handleClose">

        <div>
            <template v-for="mvc in mvcs">
                <h4 v-text="mvc.name"></h4>

                <template v-if="mvc.controller">
                    <p>Controller</p>
                    <ac-high-light v-model="mvc.controller"></ac-high-light>
                </template>

                <template v-if="mvc.service">
                    <p>Service</p>
                    <ac-high-light v-model="mvc.service"></ac-high-light>
                </template>

                <template v-if="mvc.serviceImpl">
                    <p>ServiceImpl</p>
                    <ac-high-light v-model="mvc.serviceImpl"></ac-high-light>
                </template>

                <template v-if="mvc.mapper">
                    <p>Mapper</p>
                    <ac-high-light v-model="mvc.mapper"></ac-high-light>
                </template>

                <br><br>
                <hr>
            </template>
        </div>


        <div slot="footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="success" @click="saveClick">保存文件</el-button>
        </div>

        <template>
            <ac-select-mvc-dialog
                    v-if="ui.selectMVCDialog.visible"
                    :dialogVisible="ui.selectMVCDialog.visible"
                    :dialogVisible.sync="ui.selectMVCDialog.visible"
                    @onSubmit="handleSelectSubmit"
                    @onClose="handleClose">

            </ac-select-mvc-dialog>
        </template>
    </el-dialog>
</template>

<script type="text/ecmascript-6">
    import AcSelectMvcDialog from "./SelectMVCDialog";
    import controller from '../model/controller';
    import service from '../model/service';
    import serviceImpl from '../model/serviceImpl';

    import path from 'path';
    import fs from 'fs';
    import AcHighLight from "../common/HighLightTextArea";

    /**
     * @author 白雨浓
     * @date 2018/1/17 19:50
     *
     * 生成MVC 窗口
     **/
    export default{
        components: {
            AcHighLight,
            AcSelectMvcDialog
        },
        name: 'AcCreateMvcDialog',
        props: ['dialogVisible', 'names', 'config',],
        data(){
            return {
                ui: {selectMVCDialog: {visible: false}},
                visible: this.dialogVisible,
                selects: [], /*层*/
                mvcs: [],
            }
        },
        mounted(){
            this.$nextTick(() => {
                this.ui.selectMVCDialog.visible = true;
            })
        },
        methods: {
            saveClick(){
                let projectPath = this.config.projectPath;

                if (projectPath[projectPath.length - 1] !== '/')
                    projectPath += '/';
                projectPath += 'src/main/java/';
                projectPath += this.config.packageName.replace(/\./g, '/');

                if (!fs.existsSync(path.join(projectPath))) {
                    this.$$msg.error('工程目录不存在，请检查基础设置-ProjectPath');
                    return;
                }

                for (const mvc of this.mvcs) {
                    let clsName = this.$str.toCamel(mvc.name);
                    clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

                    this.writeFile(projectPath, `controller`, `${clsName}Controller`, mvc.controller);
                    this.writeFile(projectPath, `service`, `${clsName}Service`, mvc.service);
                    this.writeFile(projectPath, `service/impl`, `${clsName}ServiceImpl`, mvc.serviceImpl);
                }
                this.handleClose();
            },
            writeFile(projectPath, packageName, filename, text){
                const filePath = path.join(projectPath + `/${packageName}/`, filename + '.java');

                if (fs.existsSync(filePath)) {
                    this.$notify({
                        title: '提示',
                        message: `${filename}.java 文件已经存在！`,
                        type: 'error',
                        duration: 0
                    });
                    return false;
                }
                fs.writeFileSync(filePath, text);
                return true;
            },
            handleSelectSubmit(selects){
                this.selects = selects;
                this.mvcs = [];
                this.names.forEach(name => {
                    let mvc = {name: name};

                    if (this.selects.indexOf('Controller') >= 0) {
                        mvc.controller = controller.controller(this, name);
                    }

                    if (this.selects.indexOf('Service') >= 0) {
                        mvc.service = service.service(this, name);
                    }

                    if (this.selects.indexOf('ServiceImpl') >= 0) {
                        mvc.serviceImpl = serviceImpl.serviceImpl(this, name);
                    }

                    this.mvcs.push(mvc);

                    /*保存最近生成的MVC，用于Mapper快速生成*/
                    this.$storage.session.setItem('recent-mvc', {table: name});
                });
            },
            /** 关闭处理 */
            handleClose(){
                this.visible = false;
                setTimeout(() => this.$emit('update:dialogVisible', false), 300);
            }
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .create-mvc-dialog {
    }
</style>